package com.chzc.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chzc.po.Role;
import com.chzc.sys.req.RoleREQ;
import com.chzc.sys.service.IRoleMenuService;
import com.chzc.sys.service.IRoleService;
import com.chzc.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-24
 */
@Api(value = "角色管理接口(私有)", tags = "角色管理接口(私有)")
@RestController
@RequestMapping("/pri/role")
public class RoleController {


    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleMenuService roleMenuService;

    @SaCheckPermission("role:search")
    @ApiOperation("根据角色名称查询列表接口")
    @PostMapping("/search")
    public IPage<Role> search(@RequestBody RoleREQ req) {
        return roleService.queryPage(req);
    }

    @SaCheckPermission("role:add")
    @ApiOperation("新增角色信息接口")
    @PostMapping
    public Result save(@RequestBody Role role) {
        roleService.save(role);
        return Result.addSuccess();
    }

    @SaCheckPermission("role:search")
    @ApiOperation("查询角色详情接口")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true)
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id) {
        return Result.querySuccess(roleService.getById(id));
    }

    @SaCheckPermission("role:edit")
    @ApiOperation("修改角色信息接口")
    @PutMapping
    public Result update(@RequestBody Role role) {
        role.setUpdateDate(new Date());
        roleService.updateById(role);
        return Result.editSuccess();
    }


    @ApiOperation("根据角色id查询拥有的菜单ids接口")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true)
    @GetMapping("/{id}/menu/ids")
    public List<String> findMenuIdsById(@PathVariable("id") String id) {
        return roleService.findMenuIdsById(id);
    }


    @SaCheckPermission("role:delete")
    @ApiOperation("通过角色ID删除角色接口")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        return roleService.deleteById(id);
    }



    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "角色ID", required = true), @ApiImplicitParam(name = "menuIds", value = "菜单id集合", required = true, allowMultiple = true, dataType = "String")})
    @ApiOperation("新增角色菜单权限数据接口")
    @PostMapping("/{id}/menu/save")
    public Result saveRoleMenu(@PathVariable("id") String id, @RequestBody List<String> menuIds) {
        return roleMenuService.saveRoleMenu(id, menuIds);
    }



}
